package com.hamizhong.iflowers.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.hamizhong.iflowers.db.dao.hibernate.IPurposeDao;
import com.hamizhong.iflowers.db.entity.Purpose;
import com.hamizhong.iflowers.service.IPurposeService;
import com.hamizhong.iflowers.service.base.MyBaseService;
import com.hamizhong.iflowers.utils.MyErrorMsgConstants;
import com.hamizhong.iflowers.web.dto.IOPResultDto;
import com.hamizhong.iflowers.web.vo.IObjectVO;
import com.hamizhong.iflowers.web.vo.PurposeVO;

public class PurposeServiceImpl extends MyBaseService implements IPurposeService {
	private IPurposeDao iPurposeDao = null;

	public IPurposeDao getiPurposeDao() {
		return iPurposeDao;
	}
	public void setiPurposeDao(IPurposeDao iPurposeDao) {
		this.iPurposeDao = iPurposeDao;
	}
	
	public IOPResultDto<IObjectVO> findAll(Integer pageNum, int pageSize) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		List<Purpose> purposeList = new ArrayList<Purpose>();
		purposeList = iPurposeDao.findPurpose(pageNum, pageSize);
		int totalPurposeCnt = iPurposeDao.findTotalPurposeCnt();
		List<PurposeVO> purposeVOList = new ArrayList<PurposeVO>();
		PurposeVO purposeVO = null;
		for (Purpose purpose : purposeList) {
			purposeVO = new PurposeVO();
			BeanUtils.copyProperties(purpose, purposeVO);
			purposeVOList.add(purposeVO);
		}
		IObjectVO iObjectVO = new IObjectVO();
		iObjectVO.setPurposeVOList(purposeVOList);
		iObjectVO.setTotalPurposeCnt(totalPurposeCnt);
		dto.addToResultList(iObjectVO);
		dto.setSuccess(true);
		return dto;
	}
	public IOPResultDto<IObjectVO> addPurpose(PurposeVO purposeVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		Purpose purpose = new Purpose();
		try {
			BeanUtils.copyProperties(purposeVO, purpose);
			iPurposeDao.add(purpose);
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			dto.setSuccess(false);
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_ADD_OPERATE);
		}
		return dto;
	}
	public IOPResultDto<IObjectVO> updatePurpose(PurposeVO purposeVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		Purpose purpose = new Purpose();
		try {
			BeanUtils.copyProperties(purposeVO, purpose);
			iPurposeDao.update(purpose);
			dto.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
			dto.setSuccess(false);
			dto.setErrorMSG(MyErrorMsgConstants.FAILED_TO_UPDATE_OPERATE);
		}
		return dto;
	}
	public IOPResultDto<IObjectVO> deletePurpose(PurposeVO purposeVO) {
		IOPResultDto<IObjectVO> dto = new IOPResultDto<IObjectVO>();
		try {
			String purposeId = purposeVO.getPurposeId();
			Purpose purpose = iPurposeDao.findById(purposeId);
			if(null != purpose) {
				this.iPurposeDao.delete(purpose);
				dto.setSuccess(true);
			}else {
				dto.setSuccess(false);
				dto.setErrorMSG(MyErrorMsgConstants.NOT_EXIST_DATA_FOR_OPERATE);
			}
		} catch (Exception e) {
			e.printStackTrace();
			dto.setErrorMSG(MyErrorMsgConstants.ERR_SYSTEM);
			dto.setSuccess(false);
		}
		return dto;
	}
	
}
